package com.zkingsoft.zkingbase.activities;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.TextView;
import android.widget.Toast;

import com.zkingsoft.zkingbase.R;
import com.zkingsoft.zkingbase.base.BaseActivity;
import com.zkingsoft.zkingbase.bean.User;
import com.zkingsoft.zkingbase.bean.resp.BaseResp;
import com.zkingsoft.zkingbase.constant.Constant;
import com.zkingsoft.zkingbase.manager.GreenDaoManager;
import com.zkingsoft.zkingbase.manager.OkHttpManager;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;

public class ModifyNameActivity extends BaseActivity
{
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.toolbar_title)
    TextView title;
    @BindView(R.id.toolbar_item)
    TextView item;
    @BindView(R.id.text)
    TextView text;

    private OkHttpManager manager;
    private User user;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        initToolBar();
        user = (User) getIntent().getSerializableExtra("info");
        if(user != null)
        {
            text.setText(user.suAliases);
        }
        manager = OkHttpManager.getInstance();
    }

    /***
     * 配置toolbar
     */
    private void initToolBar()
    {
        toolbar.setNavigationIcon(R.drawable.back_selector);
        toolbar.setNavigationOnClickListener(view -> finish());
        adjustToolbar(toolbar);
        title.setText("修改昵称");
        title.setTextColor(getResources().getColor(R.color.white));
        toolbar.setBackgroundColor(getResources().getColor(R.color.DeepOrangeGradeA400));
        item.setText("保存");
        item.setOnClickListener((v)->update());
    }

    private void update()
    {
        if(!TextUtils.isEmpty(text.getText()))
        {
            String newAliase = text.getText().toString();
            user.setSuAliases(newAliase);
            Map<String,String> param = new HashMap<>();
            param.put("id", String.valueOf(user.suId));
            param.put("aliase", text.getText().toString());
            manager.doPost(Constant.WEB_BASE + "user/modifyAliase", param,new OkHttpManager.ResultCallback<BaseResp>()
            {
                @Override
                public void onError(String simpleMsg, Exception e)
                {
                    Toast.makeText(ModifyNameActivity.this, simpleMsg, Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onSuccess(BaseResp response)
                {
                    GreenDaoManager m = GreenDaoManager.getInstance(ModifyNameActivity.this, "appdb.db");
                    m.getDaoSession().getUserDao().update(user);
                    rxPost("newUser", user);
                    finish();
                }
            });
        }
    }

    @Override
    public int getContentView()
    {
        return R.layout.activity_modify_name;
    }

}
